import numpy as np
from matplotlib import pyplot as plt
from sklearn import datasets
from sklearn.linear_model import LogisticRegression

"""加载内部数据集"""
iris = datasets.load_iris()
print(list(iris.keys()))

"""属于当前类别的为1，属于其他类别的为0"""
X = iris['data'][:, 3:]  # 选择其中一个特征
# print(X)
y = (iris['target'] == 2).astype(np.int32)
# print(y)

"""实例化, 训练模型"""
log_res = LogisticRegression()
log_res.fit(X, y)

"""构造测试数据"""
X_new = np.linspace(0, 3, 1000).reshape(-1, 1)
y_proba = log_res.predict_proba(X_new)  # 得到概率值，属于 & 不属于

"""作图决策边界"""
plt.figure(figsize=(12, 5))
decision_boundary = X_new[y_proba[:, 1] >= 0.5][0]
plt.plot([decision_boundary, decision_boundary], [-1, 2], 'k:', linewidth=2)
plt.plot(X_new, y_proba[:, 1], 'g-', label='Iris-Virginica')
plt.plot(X_new, y_proba[:, 0], 'b--', label='Not Iris-Virginica')
plt.arrow(decision_boundary, 0.08, -0.3, 0, head_width=0.05, head_length=0.1, fc='b', ec='b')
plt.arrow(decision_boundary, 0.92, 0.3, 0, head_width=0.05, head_length=0.1, fc='g', ec='g')
plt.text(decision_boundary + 0.02, 0.15, 'Decision Boundary', fontsize=16, color='k', ha='center')
plt.xlabel('Peta width(cm)', fontsize=16)
plt.ylabel('y_proba', fontsize=16)
plt.axis([0, 3, -0.02, 1.02])
plt.legend(loc='center left', fontsize=16)
plt.show()


